package br.com.ciandt.android.bd.ex.g;

import android.app.Application;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class DbCreator {

	private static final String DATABASE_NAME = "APP";
	private Context context;
	
	public DbCreator(Context context) {
		this.context = context;
	}
	
	public void criarBanco() {
		SQLiteDatabase sqliteDb = null;
		
		try {
			sqliteDb = context.openOrCreateDatabase(DATABASE_NAME, Application.MODE_WORLD_WRITEABLE, null);
			
			StringBuilder tabelaPodCast = new StringBuilder();
			tabelaPodCast.append("DROP TABLE IF EXISTS PODCAST");
			sqliteDb.execSQL(tabelaPodCast.toString());

			tabelaPodCast = new StringBuilder();
			tabelaPodCast.append("CREATE TABLE ");
			tabelaPodCast.append(" PODCAST ");
			tabelaPodCast.append(" ( ");
			tabelaPodCast.append(" ID integer primary key autoincrement, ");
			tabelaPodCast.append(" DESCRICAO varchar(200), ");
			tabelaPodCast.append(" URL varchar(200), ");
			tabelaPodCast.append(" DOWNLOAD_AUTOMATICO boolean,");
			tabelaPodCast.append(" ATIVO boolean ");
			tabelaPodCast.append(" ) ");
			sqliteDb.execSQL(tabelaPodCast.toString());
			Toast.makeText(context, "Tabela PODCAST CRIADA COM SUCESSO.", Toast.LENGTH_SHORT).show();
			sqliteDb.execSQL("INSERT INTO PODCAST (DESCRICAO, URL, DOWNLOAD_AUTOMATICO, ATIVO) VALUES ('ASFAVFEAFV', 'WWW.', 1, 0)");
			
			
			StringBuilder tabelaPodCastFile = new StringBuilder();
			tabelaPodCastFile.append("DROP TABLE IF EXISTS PODCAST_FILE");
			sqliteDb.execSQL(tabelaPodCastFile.toString());

			tabelaPodCastFile = new StringBuilder();
			tabelaPodCastFile.append("CREATE TABLE ");
			tabelaPodCastFile.append(" PODCAST_FILE ");
			tabelaPodCastFile.append(" ( ");
			tabelaPodCastFile.append(" ID integer primary key autoincrement, ");
			tabelaPodCastFile.append(" ID_PODCAST integer, ");
			tabelaPodCastFile.append(" URL varchar(200), ");
			tabelaPodCastFile.append(" NOVO boolean,");
			tabelaPodCastFile.append(" ATIVO boolean, ");
			tabelaPodCastFile.append(" NOTA integer,");
			tabelaPodCastFile.append(" FAVORITOS boolean,");
			tabelaPodCastFile.append(" FOREIGN KEY (ID_PODCAST) REFERENCES PODCAST (ID) ");
			tabelaPodCastFile.append(" ) ");
			sqliteDb.execSQL(tabelaPodCastFile.toString());
			Toast.makeText(context, "Tabela PODCAST_FILE CRIADA COM SUCESSO.", Toast.LENGTH_SHORT).show();
			sqliteDb.execSQL("INSERT INTO PODCAST_FILE (URL, NOVO, ATIVO, NOTA, FAVORITOS) VALUES ('WWW.', 1, 0, 1, 0)");
		} finally {
			if(sqliteDb != null) {
				sqliteDb.close();
			}
		}
	}
	
	public void testarSelect() {
		SQLiteDatabase sqliteDb = context.openOrCreateDatabase(DATABASE_NAME, Application.MODE_WORLD_WRITEABLE, null);;
		Cursor c = sqliteDb.query("PODCAST", new String[]{"DESCRICAO", "URL", "DOWNLOAD_AUTOMATICO","ATIVO" }, null, null, null, null, null);
		if(c != null) {
			while(!c.isAfterLast()) {
				if(c.getPosition() == -1) {
					c.moveToFirst();
				}
				String descricao = c.getString(c.getColumnIndex("DESCRICAO"));
				Integer automatico = c.getInt(c.getColumnIndex("DOWNLOAD_AUTOMATICO"));
				Toast.makeText(context, "Registro Recuperado: "+descricao+ " - "+automatico, Toast.LENGTH_SHORT).show();
				c.moveToNext();
			}
		}
		
		c = sqliteDb.query("PODCAST_FILE", new String[]{"URL", "NOVO","ATIVO","NOTA","FAVORITOS" }, null, null, null, null, null);
		if(c != null) {
			while(!c.isAfterLast()) {
				if(c.getPosition() == -1) {
					c.moveToFirst();
				}
				String descricao = c.getString(c.getColumnIndex("URL"));
				Integer automatico = c.getInt(c.getColumnIndex("NOVO"));
				Toast.makeText(context, "Registro Recuperado: "+descricao+ " - "+automatico, Toast.LENGTH_SHORT).show();
				c.moveToNext();
			}
		}
	}
	
}
